/*
Copyright 2021 LuJun

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished 
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { stringToBuffer, mergeBuffer } from '../core/utils/js';

/**
 * @zh
 * 缓冲写入对象
 */
export class BufferWriter {
    /**
     * @zh
     * 缓冲数组
     */
    private _buffers: ArrayBuffer[] = [];

    /**
     * @zh
     * 缓冲大小
     */
    private _size: number = 0;

    /**
     * @zh
     * 获得缓冲大小
     */
    get size() {
        return this._size;
    }

    /**
     * @zh
     * 填充指定数量的字符
     *
     * @param count - 字节数
     * @param char - 数值
     */
    fill(count: number, char: number) {
        if (count > 0) {
            let buff = new ArrayBuffer(count);
            new Int8Array(buff).fill(char, 0, -1);
            this._buffers.push(buff);
            this._size += count;
        }
    }

    /**
     * @zh
     * 缓冲区对齐
     *
     * @param size - 对齐字节数
     * @param char - 填充数值
     */
    alignment(size: number, char: number) {
        let rem = this._size % size;
        if (rem !== 0) {
            this.fill(rem, char);
        }
    }

    /**
     * @zh
     * 写入 Byte
     *
     * @param byte - 数据
     */
    writeByte(byte: number) {
        let buff = new ArrayBuffer(1);
        new DataView(buff).setInt8(0, byte);
        this._buffers.push(buff);
        this._size += 1;
    }

    /**
     * @zh
     * 写入 unsigned Byte
     *
     * @param byte - 数据
     */
    writeUByte(byte: number) {
        let buff = new ArrayBuffer(1);
        new DataView(buff).setUint8(0, byte);
        this._buffers.push(buff);
        this._size += 1;
    }

    /**
     * @zh
     * 写入 Short
     *
     * @param Short - 数据
     */
    writeShort(short: number) {
        let buff = new ArrayBuffer(2);
        new DataView(buff).setInt16(0, short, true);
        this._buffers.push(buff);
        this._size += 2;
    }

    /**
     * @zh
     * 写入 unsigned Short
     *
     * @param Short - 数据
     */
    writeUShort(short: number) {
        let buff = new ArrayBuffer(2);
        new DataView(buff).setUint16(0, short, true);
        this._buffers.push(buff);
        this._size += 2;
    }

    /**
     * @zh
     * 写入 Long
     *
     * @param Long - 数据
     */
    writeLong(long: number) {
        let buff = new ArrayBuffer(4);
        new DataView(buff).setInt32(0, long, true);
        this._buffers.push(buff);
        this._size += 4;
    }

    /**
     * @zh
     * 写入 unsigned Long
     *
     * @param Long - 数据
     */
    writeULong(long: number) {
        let buff = new ArrayBuffer(4);
        new DataView(buff).setUint32(0, long, true);
        this._buffers.push(buff);
        this._size += 4;
    }

    /**
     * @zh
     * 写入 unsigned Float
     *
     * @param Float - 数据
     */
    writeFloat(float: number) {
        let buff = new ArrayBuffer(4);
        new DataView(buff).setFloat32(0, float, true);
        this._buffers.push(buff);
        this._size += 4;
    }

    /**
     * @zh
     * 写入 unsigned Double
     *
     * @param Double - 数据
     */
    writeDouble(double: number) {
        let buff = new ArrayBuffer(8);
        new DataView(buff).setFloat64(0, double, true);
        this._buffers.push(buff);
        this._size += 8;
    }

    /**
     * @zh
     * 写入数据
     *
     * @param buffer - 缓冲
     */
    writeData(buffer: ArrayBuffer) {
        this._buffers.push(buffer);
        this._size += buffer.byteLength;
    }

    /**
     * @zh
     * 写入UTF16LE字符串
     *
     * @param str - 字符串
     */
    writeString(str: string) {
        let buff = stringToBuffer(str);
        this.writeUShort(buff.byteLength);
        this.writeData(buff);
    }

    /**
     * @zh
     * 清空缓冲
     */
    clear() {
        this._buffers = [];
        this._size = 0;
    }

    /**
     * @zh
     * 获得数据
     *
     * @returns 最终的Buffer数据
     */
    getData() {
        return mergeBuffer(...this._buffers);
    }
}
